package cpnt.iris
{
	import bean.ItemPropertyBean;
	
	import flash.events.Event;
	
	import mx.containers.HBox;
	import mx.controls.AdvancedDataGrid;
	import mx.controls.Image;
	import mx.controls.Label;
	import mx.controls.advancedDataGridClasses.AdvancedDataGridListData;
	import mx.controls.listClasses.BaseListData;
	import mx.controls.listClasses.IDropInListItemRenderer;

	public class RendererIconLabel extends HBox implements IDropInListItemRenderer
	{
		private var _icon:Image;
		private var _label:Label;
		public function RendererIconLabel()
		{
			super();
			
			super.horizontalScrollPolicy = "false";
			super.verticalScrollPolicy = "false";
			super.setStyle("verticalAlign", "middle");
			
			_icon = new Image();
			_icon.addEventListener(Event.INIT, iconInitListener);
			this.addChild(_icon);
			
			_label = new Label();
			this.addChild(_label);
		}
		private var _iconDir:String;
		public function set iconDir(data:String):void
		{
			_iconDir = data;
		}
		
		private var _iconDataFields:Array;
		public function set iconDataFields(data:String):void
		{
			_iconDataFields = data.split(".");
		}
		
		private var _labelDataFields:Array;
		public function set labelDataFields(data:String):void
		{
			_labelDataFields = data.split(".");
		}
		
		private var _itemPropertyBean:ItemPropertyBean;
		
		private var _listData:AdvancedDataGridListData;
		public function set listData(value:BaseListData):void
		{
			_listData = value as AdvancedDataGridListData;
			
			_iconWidth = (_listData.owner as AdvancedDataGrid).rowHeight;
			_iconHeight = (_listData.owner as AdvancedDataGrid).rowHeight;
			
			_itemPropertyBean = _listData.item as ItemPropertyBean;
			
			loadIcon();
			initLabel();
		}
		private function loadIcon():void
		{
			var temp:Object = _itemPropertyBean;
			for each(var field:String in _iconDataFields)
				temp = temp[field];
			
			if(temp != null)
			{
				_icon.source = "C:/Google Code/air-armory/trunk/flex-project/Iris/assets/icon/" + _iconDir + "/" + temp + ".jpg";
				_icon.includeInLayout = true;
			}
			else
			{
				_icon.unloadAndStop();
				_icon.includeInLayout = false;
			}
		}
		private function initLabel():void
		{
			var temp:Object = _itemPropertyBean;
			for each(var field:String in _labelDataFields)
				temp = temp[field];
				
			_label.text = temp as String;
		}
		
		private var _iconWidth:int;
		private var _iconHeight:int;
		private function iconInitListener(event:Event):void
		{
			var image:Image = event.target as Image;
			var yScale:Number = _iconHeight / image.contentHeight;
			var xScale:Number = _iconWidth / image.contentWidth;
			
			if(yScale < 1 || xScale < 1)
			{
				var scale:Number = Math.min(yScale, xScale);
				
				image.height = image.contentHeight * scale;
				image.width = image.contentWidth * scale;
			}
		}
		public function get listData():BaseListData
		{
			return _listData;
		}
	}
}
